package com.fgbf.todo.Activities;

import com.fgbf.todo.Data.SharedData.SharedData;
import com.fgbf.todo.Managers.LoginManager;
import com.fgbf.todo.Managers.NotificationManager;
import com.fgbf.todo.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * GUI Class for the Login Screen
 * @author Warren Shenk
 * @date 2/20/2012
 */
public class LoginActivity extends Activity {
	
	///////////////////////////////////////////////////////////////////////////////////
	// INSTANCE DATA
	///////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Checkboxes
	 */
	private CheckBox persistUsrChBx;
	/**
	 * Buttons
	 */
	private Button loginBttn, newUserBttn;
	/**
	 * EditTexts
	 */
	private EditText userEtxt, passEtxt;
	/**
	 * Instance of Login Manager to handle login situations
	 */
	private LoginManager lgMgr;
	
	
	///////////////////////////////////////////////////////////////////////////////////
	// METHODS
	///////////////////////////////////////////////////////////////////////////////////

    /** 
     * Called when the activity is first created. 
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {    	
        super.onCreate(savedInstanceState);

        lgMgr = new LoginManager(LoginActivity.this);
		        
		if (lgMgr.checkStayLoggedIn(LoginActivity.this)) {
			Intent i = new Intent(LoginActivity.this, TaskActivity.class); 
			startActivity(i);
			LoginActivity.this.finish();
		}
		
        setContentView(R.layout.login_screen);
        
        persistUsrChBx = (CheckBox) findViewById(R.id.chbx_persist);
        
        loginBttn = (Button) findViewById(R.id.bttn_login);
        loginBttn.setOnClickListener(loginListener);
        
        newUserBttn = (Button) findViewById(R.id.bttn_newuser);
        newUserBttn.setOnClickListener(newUserListener);
        
        userEtxt = (EditText) findViewById(R.id.etxt_username);
        
        passEtxt = (EditText) findViewById(R.id.etxt_password);

    }
    
    
    
    ///////////////////////////////////////////////////////////////////////////////////
    // LISTENERS
    ///////////////////////////////////////////////////////////////////////////////////
    
    /**
     * OnClickListener that responds to the New User Button
     */
    private OnClickListener newUserListener = new OnClickListener() {
    	public void onClick (View v) {
    		
    		//Starts a NewUserActivity, Pauses the current LoginActivity
    		startActivity(new Intent(v.getContext(), NewUserActivity.class)); 
    		LoginActivity.this.finish();
    	}
    };
    //---------------------------------------------------------------------------------
    
    /**
     * OnClickListener that responds to the Login Button
     */
    private OnClickListener loginListener = new OnClickListener() {
    	public void onClick (View v) {
    		String userStr = userEtxt.getText().toString();
    		String passStr = passEtxt.getText().toString();
    		
    		if (lgMgr.checkLogin(userStr, passStr)) {
    			//Updates the Last User Logged in && the stay logged in boolean
    			lgMgr.updateLoginData(userStr, persistUsrChBx.isChecked(), LoginActivity.this);
    			
    			//Starts a TaskActivity, Pauses the current LoginActivity
    			startActivity(new Intent(v.getContext(), TaskActivity.class)); 
    			LoginActivity.this.finish();
    		}
    		else if (lgMgr.userNameExists(userStr)) {
    			//Notify user that password was incorrect
    			NotificationManager.alertUserOK(LoginActivity.this, "Password Incorrect");
    		}
    		else {
    			//Notify user that username did not exist
    			NotificationManager.alertUserOK(LoginActivity.this, "User Name Does Not Exist");
    		}
    		
    	}
    };
    //---------------------------------------------------------------------------------



    
    
    	
}